import cv2
import sys
import numpy
from pathlib import Path
from tqdm import tqdm

try:
    source_file = sys.argv[1]
except:
    print("Please indicate your image to inverte.")
    sys.exit()

if Path(source_file).is_dir():
    file_type = '*.jpg'
    source_files = Path(source_file).glob(file_type)
    source_files = [str(sourceFile) for sourceFile in source_files]
    source_files = sorted(source_files)
    source_files = tqdm(source_files)
    for sourceFile in source_files: 
        targetFile = ''.join([sourceFile.split('.')[0],'_inverted.jpg'])
        img = cv2.imread(sourceFile)
        new_img = 255 - img
        cv2.imwrite(targetFile,new_img)
else:
    try:
        target_file = sys.argv[2]
    except:
        print("Please indicate your target name.")
        sys.exit()
    img = cv2.imread(source_file)
    new_img = 255 - img
    cv2.imwrite(target_file,new_img)
